﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SiChuan.FormsApp.AppCode {

    public class Constants {

        /// <summary>
        /// 请求ID
        /// </summary>
        public static string RequestId { get; set; }

        public static string AppVersion {
            get {
                string ver = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                return ver;
            }
        }

        #region 自动更新

        /// <summary>
        /// 更新号
        /// </summary>
        public static long UpdateVersion {
            get {
                var ver = ConfigurationManager.AppSettings["updateVersion"];

                long version = 1;
                long.TryParse(ver, out version);

                return version;
            }
        }
        /// <summary>
        /// 远程更新要找的版本信息
        /// </summary>
        public static string UpdateUrl {
            get {
                var str = ConfigurationManager.AppSettings["updateUrl"];
                if (string.IsNullOrWhiteSpace(str)) {
                    str = "http://flamezn.w1.luyouxia.net/version.txt";
                }
                return str;
            }
        }
        /// <summary>
        /// 下载的文件包
        /// </summary>
        public static string UpdateDownloadUrl {
            get {
                var str = ConfigurationManager.AppSettings["updateDownloadUrl"];
                if (string.IsNullOrWhiteSpace(str)) {
                    str = "http://flamezn.w1.luyouxia.net/UpdateFile.uda";
                }
                return str;
            }
        }

        public static void SetUpdateVersion(long version) {
            // 将值修改后写入回文件.
            Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            // 如果没有就添加新的
            if (configuration.AppSettings.Settings["updateVersion"] == null) { configuration.AppSettings.Settings.Add("updateVersion", ""); }
            configuration.AppSettings.Settings["updateVersion"].Value = version.ToString();
            configuration.Save(ConfigurationSaveMode.Modified);

            //// 再次读取该应用程序设置以确保更改已生效。
            //string newValue = ConfigurationManager.AppSettings["key"];
        }
        #endregion

        //public static IFreeSql FSql {
        //    get {
        //        var path = AppDomain.CurrentDomain.BaseDirectory + "logs.db";
        //        //var path = AppDomain.CurrentDomain.BaseDirectory + "logs.accdb";
        //        //var path = AppDomain.CurrentDomain.BaseDirectory + "logs.mdb";
        //        //Provider=Microsoft.Jet.OleDb.4.0;Data Source=d:/accdb/2003.mdb
        //        return new FreeSqlBuilder()
        //                    //.UseConnectionString(DataType.MsAccess, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path)
        //                    //.UseConnectionString(DataType.MsAccess, "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + path)
        //                    //.UseConnectionString(DataType.Sqlite, "Journal Mode=wal;Data Source=" + path)
        //                    .UseConnectionString(DataType.Sqlite, "Data Source=" + path)
        //                    .UseAutoSyncStructure(true) //自动同步实体结构到数据库
        //                    .Build();
        //    }
        //}

        public static string XmlFilePath {
            get {
                var xmlfilepath = ConfigurationManager.AppSettings["xmlfilepath"];
                if (string.IsNullOrWhiteSpace(xmlfilepath)) {
                    xmlfilepath = AppDomain.CurrentDomain.BaseDirectory + "file\\";
                }
                return xmlfilepath;
            }
        }

        /// <summary>
        /// 针对win10-所说的版本太低
        /// </summary>
        public static bool IsWin10 {
            get {
                var m = ConfigurationManager.AppSettings["iswin10"];
                if (!string.IsNullOrWhiteSpace(m) && m == "1") {
                    return true;
                }
                return false;
            }
        }

        public static bool IsDebug {
            get {
                var xmlfilepath = ConfigurationManager.AppSettings["isdebug"];
                if (!string.IsNullOrWhiteSpace(xmlfilepath) && xmlfilepath == "1") {
                    return true;
                } else {
                    return false;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static string ApiUrl {
            get {
                var xmlfilepath = ConfigurationManager.AppSettings["apiurl"];
                if (!string.IsNullOrWhiteSpace(xmlfilepath)) {
                    return xmlfilepath;
                } else {
                    throw new Exceptions.WarnException("apiurl为空");
                }
            }
        }

        /// <summary>
        /// 是否64位系统
        /// </summary>
        public static bool IsX64 {
            get {
                var xmlfilepath = ConfigurationManager.AppSettings["isx64"];
                if (!string.IsNullOrWhiteSpace(xmlfilepath) && xmlfilepath == "1") {
                    return true;
                } else {
                    return false;
                }
            }
        }

        /// <summary>
        /// 每次提交后等待的时间
        /// </summary>
        public static int WaitingTime {
            get {
                var timer = ConfigurationManager.AppSettings["waitingtime"];
                var t = 30000;
                try {
                    if (int.TryParse(timer, out t)) {
                        return t;
                    } else {
                        return t;
                    }

                } catch {
                    return 30000;
                }
            }
        }

    }
}
